package com.ligang.controller;

import com.ligang.entity.Video;
import com.ligang.entity.VideoOrder;
import com.ligang.service.VideoOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * (VideoOrder)表控制层
 *
 * @author makejava
 * @since 2021-06-30 14:31:20
 */
@RestController
@RequestMapping("/api/v1/videoOrder")
public class VideoOrderController {
    /**
     * 服务对象
     */
    @Resource
    private VideoOrderService videoOrderService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public VideoOrder selectOne(Integer id) {
        return this.videoOrderService.queryById(id);
    }


//    @GetMapping("selectByVideoId")
//    public Video selectByVideoId(Integer videoId) {
//        RestTemplate restTemplate = new RestTemplate();
//        return restTemplate.getForObject("http://127.0.0.1:8090/api/v1/video/find_by_id?videoId="+videoId, Video.class);
//    }

    @RequestMapping("selectByVideoId")
    public VideoOrder save(int videoId){
//        List<ServiceInstance> list = discoveryClient.getInstances("video-service");

//        ServiceInstance serviceInstance = list.get(0);

        Video video = restTemplate.getForObject("http://video-service/api/v1/video/find_by_id?videoId="+videoId,Video.class);

        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setVideoTitle(video.getTitle());
        videoOrder.setVideoId(video.getId());

        videoOrder.setServerInfo(video.getServerInfo());
        return videoOrder;
    }

}